package com.wzcl.app.model.vo;

import com.alibaba.fastjson.JSON;
import com.wzcl.app.dao.model.UserGroup;
import com.wzcl.app.model.dto.UserDto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@EqualsAndHashCode(callSuper = false)
@Data
public class UserGroupVo extends BaseVo {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "名称")
    private String name;

    @ApiModelProperty(value = "标签")
    private List<String> tag;

    @ApiModelProperty(value = "是否自动生成")
    private Boolean isGenerate;
    private String isGenerateStr;

    //----------查询条件------------
    @ApiModelProperty(value = "用户真实姓名")
    private String realName;

    @ApiModelProperty("手机")
    private String phone;

    @ApiModelProperty(value = "用户状态")
    private Integer userState;

    @ApiModelProperty(value = "角色类型")
    private Integer type;
    //---------条件结束-------------

    @ApiModelProperty(value = "用户Id")
    private List<AppUserVo> userList;


    private List<OrderDetailVo> detailList;

    private UserGroupVo(){

    }

    public static UserGroupVo create(UserGroup userGroup){
        UserGroupVo userGroupVo = new UserGroupVo();
        BeanUtils.copyProperties(userGroup,userGroupVo,"tag","userList");
        if(StringUtils.isNotBlank(userGroup.getTag())){
            userGroupVo.tag = Arrays.asList(userGroup.getTag().split(","));
        }
        if(StringUtils.isNotBlank(userGroup.getConditions())){
            UserDto userDto = JSON.parseObject(userGroup.getConditions(), UserDto.class);
            userGroupVo.realName = userDto.getRealName();
            userGroupVo.phone = userDto.getPhone();
            userGroupVo.userState = userDto.getUserState();
            userGroupVo.type = userDto.getType();
        }
        if(userGroup.getUserList()!=null && userGroup.getUserList().size()>0){
            userGroupVo.userList=userGroup.getUserList().stream().map(AppUserVo::create).collect(Collectors.toList());
        }
        userGroupVo.translationValueStr();
        return userGroupVo;
    }

    @Override
    public void translationValueStr() {
        Map<String, Map<Integer, String>> map = getTranslationMap();
        this.isGenerateStr = this.isGenerate?"规则生成":"自定义";
    }

    @Override
    public void processResources() {

    }
}
